home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / procssng / alv.sun / alv.lha / src / hist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-08  |  5.1 KB  |  222 lines

  1. #include <suntool/sunview.h>
  2. #include <suntool/canvas.h>
  3. #include "defs.h"
  4.  
  5. tatic short    histicon[] =
  6. {
  7. #include "../images/icons/hist.icon"
  8. };
  9. EFINE_ICON_FROM_IMAGE(icon, histicon);
  10.  
  11. #define HSIZE    128
  12. #define HEADER    10
  13. #define BOTTOM    30
  14. #define BOXSIZE    3
  15.  
  16. Frame           frame;
  17. anvas          canvas;
  18. enu            menu;
  19. ixwin         *pw;
  20. har           *filename;
  21. ouble          lhist[90000];
  22. nt             hist[90000], n = 0;
  23. nt             greylevels;
  24. har           *progname;
  25. nt             max_val, min_val;
  26. nt             winxsiz, winysiz;
  27. ixrect        *pr, *dpr;
  28. nt             vertical, logarithmic;
  29. nt             maxh;
  30. ouble          lmaxh;
  31.  
  32. #ifdef STANDALONE
  33. ain(argc, argv, envp)
  34. #else
  35. ist_main(argc, argv, envp)
  36. #endif
  37.     int             argc;
  38.     char          **argv;
  39.     char          **envp;
  40. {
  41.     register int    i, j;
  42.     int             len, dummy;
  43.     char           *itoa();
  44.     char            message[BUFSIZ];
  45.     char            buf1[BUFSIZ];
  46.     int             c;
  47.     colormap_t        colormap;
  48.  
  49.     filename = strsave("stdin");
  50.     vertical = FALSE;
  51.     logarithmic = FALSE;
  52.     progname = strsave(argv[0]);
  53.     parse_profile(&argc, argv, envp);
  54.  
  55.     while ((gc = getopt(argc, argv, "W:lvb")) != EOF)
  56.         switch (gc) {
  57.         case 'l':
  58.             logarithmic = TRUE;
  59.             break;
  60.         case 'v':
  61.             vertical = TRUE;
  62.             break;
  63.         case 'b':
  64.             bitrestrict = TRUE;
  65.             break;
  66.         case '?':
  67.             errflag++;
  68.             break;
  69.         }
  70.     if (errflag)
  71.         error((char *) 0, "Usage: %s: [-l] [-v] [-b] [infile]\n", progname);
  72.  
  73.     for (stream = 0; optind < argc; stream++, optind++)
  74.         if (stream == 0 && strcmp(argv[optind], "-") != 0) {
  75.             filename = strsave(argv[optind]);
  76.             if (freopen(argv[optind], mode[stream], f[stream]) == NULL)
  77.                 error("%s %s", PR_IO_ERR_INFILE, argv[optind]);
  78.         }
  79.  
  80.     if ((pr = pr_load(stdin, &colormap)) == NULL)
  81.         error(PR_IO_ERR_RASREAD);
  82.  
  83.     if (bitrestrict)
  84.         greylevels = calc_max(pr)+1;
  85.     else
  86.         greylevels = MAXLEVEL(pr->pr_depth);
  87.  
  88.     statistics();
  89.  
  90.     calc_winsize();
  91.  
  92.     sprintf(message, "%s: %s", (logarithmic) ? "logarithmic histogram" : "histogram", filename);
  93.     frame = window_create(NULL, FRAME,
  94.                   FRAME_LABEL, message,
  95.                   WIN_HEIGHT, winxsiz,
  96.                   WIN_WIDTH, winysiz,
  97.                   FRAME_ICON, &icon,
  98.                   FRAME_ARGS, argc, argv,
  99.                   0);
  100.     canvas = window_create(frame, CANVAS,
  101.                    CANVAS_HEIGHT, HSIZE + HEADER + BOTTOM,
  102.                    CANVAS_WIDTH, greylevels * BOXSIZE + 10,
  103.                    CANVAS_AUTO_SHRINK, FALSE,
  104.                    WIN_HORIZONTAL_SCROLLBAR, scrollbar_create(0),
  105.                    0);
  106.  
  107.     dpr = mem_create(window_get(canvas, CANVAS_HEIGHT),
  108.              window_get(canvas, CANVAS_WIDTH),
  109.              8);
  110.  
  111.     pw = canvas_pixwin(canvas);
  112.  
  113.     draw_histogram(pw);
  114.  
  115.     pw_vector(pw, 0, HSIZE + HEADER, greylevels * BOXSIZE, HSIZE + HEADER, PIX_SET, 1);
  116.     for (i = 0; i < greylevels; i++) {
  117.         len = (i % 100 == 0) ? 10 :
  118.             (i % 50 == 0) ? 10 :
  119.             (i % 10 == 0) ? 6 :
  120.             (i % 2 == 0) ? 2 : 0;
  121.         if (len)
  122.             pw_vector(pw, i * BOXSIZE, HSIZE + HEADER, i * BOXSIZE, HSIZE + HEADER + len, PIX_SET, 1);
  123.         if (i % 10 == 0)
  124.             pw_text(pw, i * BOXSIZE, HSIZE + HEADER + 17, PIX_SRC, smallfont, itoa(i));
  125.     }
  126.  
  127.     window_main_loop(frame);
  128.  
  129.     exit(0);
  130. }
  131.  
  132. alc_winsize()
  133. {
  134.     winxsiz = HSIZE + 28 + HEADER + BOTTOM;
  135.     if ((greylevels * BOXSIZE + 10) > 500)
  136.         winysiz = 500;
  137.     else
  138.         winysiz = greylevels * BOXSIZE + 10;
  139. }
  140.  
  141. tatistics()
  142. {
  143.     register int    i, j;
  144.     double          sumsq, stddev, mean;
  145.     int             cumulate, count, val, sum, median, mode;
  146.  
  147.     sumsq = 0.;
  148.     sum = 0;
  149.     max_val = 0;
  150.     min_val = 10000;
  151.  
  152.     for (j = 0; j < pr->pr_size.y; j++)
  153.         for (i = 0; i < pr->pr_size.x; i++) {
  154.             val = pr_get(pr, i, j);
  155.             max_val = MAX(max_val, val);
  156.             min_val = MIN(min_val, val);
  157.             hist[val]++;
  158.             sumsq += (val * val);
  159.             sum += val;
  160.         }
  161.     n = pr->pr_size.y * pr->pr_size.x;
  162.     mean = (double) sum / (double) n;
  163.     stddev = sqrt(sumsq / (double) n - mean * mean);
  164.  
  165.     maxh = 0;
  166.     for (i = 0; i < greylevels; i++)
  167.         maxh = MAX(maxh, hist[i]);
  168.  
  169.     i = count = 0;
  170.     while (count < n / 2)
  171.         count += hist[i++];
  172.     median = i - 1;
  173.  
  174.  
  175.     i = mode = cumulate = 0;
  176.  
  177.     for (i = 0; i < greylevels; i++)
  178.         if (hist[i] > cumulate) {
  179.             cumulate = hist[i];
  180.             mode = i;
  181.         }
  182.     fprintf(stderr, "mean       = %3g\n", mean);
  183.     fprintf(stderr, "median     = %d\n", median);
  184.     fprintf(stderr, "mode       = %d\n", mode);
  185.     fprintf(stderr, "std. dev.  = %3g\n", stddev);
  186.     fprintf(stderr, "max pixel  = %d\n", max_val);
  187.     fprintf(stderr, "min pixel  = %d\n", min_val);
  188. }
  189.  
  190. raw_histogram(pw)
  191.     Pixwin         *pw;
  192. {
  193.     int             temp, count, i, npts, dx, dy;
  194.  
  195.     npts = greylevels * 2 + 4;
  196.     dx = 0;
  197.     dy = HSIZE + HEADER;
  198.  
  199.     pw_batch_on(pw);
  200.     pw_vector(pw, dx, dy, i * BOXSIZE, dy - hist[0] * HSIZE / maxh, PIX_SRC, 1);
  201.  
  202.     if (logarithmic)
  203.         for (i = 0; i < greylevels; i++) {
  204.             lhist[i] = log((double) hist[i] + 1.0);    /* add 1 to avoid log(0) */
  205.             if (lmaxh < lhist[i])
  206.                 lmaxh = lhist[i];
  207.         }
  208.  
  209.     for (i = 0; i < greylevels; i++) {
  210.         if (logarithmic)
  211.             temp = (int) (lhist[i] * HSIZE / lmaxh);
  212.         else
  213.             temp = hist[i] * HSIZE / maxh;
  214.         pw_vector(pw, i * BOXSIZE, dy - temp, (i + 1) * BOXSIZE, dy - temp, PIX_SRC, 1);
  215.         if (vertical) {
  216.             pw_vector(pw, i * BOXSIZE, dy - temp, i * BOXSIZE, dy, PIX_SET, 1);
  217.             pw_vector(pw, (i + 1) * BOXSIZE, dy - temp, (i + 1) * BOXSIZE, dy, PIX_SET, 1);
  218.         }
  219.     }
  220.     pw_batch_off(pw);
  221. }
  222.